#!/bin/bash
# 计算 120 秒内 $1 平均读写速率

INTERVAL=120
read rx_init tx_init < <(cat /proc/diskstats|grep "$1"|awk '{print $6 * 512, $10 * 512}')
sleep ${INTERVAL}s
read rx_end tx_end < <(cat /proc/diskstats|grep "$1"|awk '{print $6 * 512, $10 * 512}')

now=$(date '+%Y-%m-%d %H:%M:%S')

rx_diff=$((rx_end - rx_init))
tx_diff=$((tx_end - tx_init))
rx_rate=$(echo "scale=4; $rx_diff / $INTERVAL / 1024" | bc)
rx_rate=$(printf "%.4f" $rx_rate)
tx_rate=$(echo "scale=4; $tx_diff / $INTERVAL / 1024" | bc)
tx_rate=$(printf "%.4f" $tx_rate)

echo $now
echo "Disk Read Data: $rx_rate kB/s"
echo "Disk Write Data: $tx_rate kB/s"
